package com.jfinal.ext.shrio;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;

import cn.erp.common.model.SystemUser;

public class ShiroUtils {
	/**
	 * 返回当前登录的认证实体ID
	 * 
	 * @return
	 */
	public static Long getUserId() {
		ShiroPrincipal principal = getPrincipal();
		if (principal != null)
			return principal.getId();
		return -1L;
	}

	public static SystemUser getUser() {
		ShiroPrincipal principal = getPrincipal();
		if (principal != null)
			return principal.getUser();
		return null;
	}

	/**
	 * 获取当前登录的认证实体
	 * 
	 * @return
	 */
	public static ShiroPrincipal getPrincipal() {
		Subject subject = SecurityUtils.getSubject();
		return (ShiroPrincipal) subject.getPrincipal();
	}

	/**
	 * 获取当前认证实体的中文名称
	 * 
	 * @return
	 */
	public static String getFullname() {
		ShiroPrincipal principal = getPrincipal();
		if (principal != null)
			return principal.toString();
		return "";
	}

	/**
	 * 获取当前认证实体的登录名称
	 * 
	 * @return
	 */
	public static String getUsername() {
		ShiroPrincipal principal = getPrincipal();
		if (principal != null)
			return principal.getUsername();
		throw new RuntimeException("user's name is null.");
	}

	/**
	 * 获取当前认证的实体部门名称
	 * 
	 * @return
	 */
	public static String getOrgName() {
		SystemUser user = getUser();
		if (user != null)
			return user.get("orgName");
		return "";
	}
}
